package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.MaterialHistoryService;
import com.ruoyi.system.service.MaterialLogService;
import com.ruoyi.system.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialHistoryServiceImpl extends ServiceImpl<MaterialHistoryMapper, MaterialHistoryStatisticsEntity> implements MaterialHistoryService {

    @Autowired
    private MaterialHistoryMapper materialHistoryMapper;

    /*
     *  出库按照天统计
     * */
    public boolean addOrUpdate(MaterialHistoryStatisticsEntity materialHistoryStatisticsEntity) {
       List<MaterialHistoryStatisticsEntity> records = materialHistoryMapper.selectList(
               new LambdaQueryWrapper<MaterialHistoryStatisticsEntity>()
                       .eq(MaterialHistoryStatisticsEntity::getDelFlag,0)
                       .eq(MaterialHistoryStatisticsEntity::getMaterialId,materialHistoryStatisticsEntity.getMaterialId())
                       .eq(MaterialHistoryStatisticsEntity::getCreateTime, materialHistoryStatisticsEntity.getCreateTime())
       );

        if (records != null && !records.isEmpty()) {
            // 创建更新条件
            LambdaUpdateWrapper<MaterialHistoryStatisticsEntity> updateWrapper = new LambdaUpdateWrapper<MaterialHistoryStatisticsEntity>()
                    .eq(MaterialHistoryStatisticsEntity::getMaterialId, materialHistoryStatisticsEntity.getMaterialId())
                    .eq(MaterialHistoryStatisticsEntity::getCreateTime, materialHistoryStatisticsEntity.getCreateTime());
            // 执行更新操作
            materialHistoryMapper.update(materialHistoryStatisticsEntity, updateWrapper);
        } else {
            materialHistoryMapper.insert(materialHistoryStatisticsEntity);
        }
        return true;
    }
}